//小仓库 关于layout的设置
import { defineStore } from "pinia";
import { getColumn, reqAddColumn, reqDeleteColumn, reqUpdateColumn, reqgetColumnNumber, reqSearchColumn, reqSearchColumnNumber } from '@/api/column'
import { ElMessage } from "element-plus";
let useColumnStore = defineStore("ColumnStore", {
    state: () => {
        return {
            columnList: [],
        };
    },
    // 异步 | 逻辑的地方
    actions: {
        async getColumnList(id: number, pageNo: number, limit: number) {
            let res = await getColumn(id, pageNo, limit);
            if (res.code == 200) {
                this.columnList = res.data
            }
        },
        async addColumnName(name: string) {
            let jsonData = {
                name: name
            }
            let columnName = JSON.stringify(jsonData)

            let res = await reqAddColumn(columnName);
            if (res.code == 200) {
                return "ok"
            }
        },
        async addSubColumn(form: any) {
            if (form.status) {
                form.status = 1
            } else {
                form.status = 0
            }
            let res = await reqAddColumn(form);
            if (res.code == 200) {
                return "ok"
            }
        },
        async deleteColumns(ids: []) {
            let res = await reqDeleteColumn(ids);
            if (res.code == 200) {
                return "ok"
            }
        },
        async updateColumn(form: any) {
            if (form.status) {
                form.status = 1
            } else {
                form.status = 0
            }
            let res = await reqUpdateColumn(form);
            if (res.code == 200) {
                return "ok"
            }
        },
        async getColumnNumber() {
            let res = await reqgetColumnNumber();
            if (res.code == 200) {
                return res.data
            }
        },
        async searchColumn(name: string, pageNo: number, limit: number) {
            let res = await reqSearchColumn(name, pageNo, limit);

            if (res.data.length == 0) {
                ElMessage.error("没有找到相关内容")
            } else {
                this.columnList = res.data
                ElMessage.success("查询成功")
            }
        },
        async searchColumnNumber(name: string) {
            let res = await reqSearchColumnNumber(name);
            if (res.code == 200) {
                return res.data
            }
        }
    },
    getters: {

    }
});

export default useColumnStore;